package com.desktopmng.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;

import com.desktopmng.core.ServiceManager;
import com.desktopmng.exception.JFossException;
import com.desktopmng.orm.DesktopContent;
import com.desktopmng.orm.DstcToRole;
import com.desktopmng.orm.Role;
import com.desktopmng.service.DesktopContentManager;
import com.desktopmng.service.DstcToRoleManager;
import com.desktopmng.service.RoleManager;
import com.desktopmng.service.UserConfigManager;
import com.desktopmng.service.UserToDstcManager;

@Results( {
		@Result(value = "/common/message.jsp"),
		@Result(name = "delInfo", value = "/jfoss2/desktop/userconfig/editRole.jsp"),
		@Result(name = "modifyInfo", value = "/jfoss2/desktop/userconfig/updateRole.jsp"),
		@Result(name = "addInfo", value = "/jfoss2/desktop/userconfig/editDstcToRoleSucess.jsp"),
		@Result(name = "searchinfoinit", value = "/jfoss2/desktop/userconfig/editRole.jsp") })
@SuppressWarnings({"unchecked","rawtypes", "serial"})
public class DstcToRoleAction extends BaseAction {
	private UserToDstcManager userToDstcManager = (UserToDstcManager) ServiceManager
			.getBean("userToDstcManager");

	private DesktopContentManager desktopContentManager = (DesktopContentManager) ServiceManager
			.getBean("desktopContentManager");

	private UserConfigManager userConfigManager = (UserConfigManager) ServiceManager
			.getBean("userConfigManager");

	private DstcToRoleManager dstcToRoleManager = (DstcToRoleManager) ServiceManager
			.getBean("dstcToRoleManager");

	private RoleManager roleManager = (RoleManager) ServiceManager
			.getBean("roleManager");

	/**
	 * 查询出所有角色，显示到页面上
	 */
	public String searchinfoinit() throws Exception {

		try {
			List roles = roleManager.getAll();
			//List<Role> superRole = roleManager.findRoleByType(new Character('2'));  //超级管理员角色
			//roles.removeAll(superRole);
			request.setAttribute("roles", roles);
		} catch (JFossException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return SEARCHINFOINIT;
	}

	/**
	 * 根据角色ID查询出所有该角色含有的桌面内容对象
	 */
	public String delInfo() throws Exception {
		Properties prop = new Properties();
		List list1 = new ArrayList();
		String roleId = request.getParameter("role");
		Role role = new Role();
		role.setId(new Long(roleId));
		prop.setProperty("roleId", roleId);
		try {
			List roles = roleManager.getAll();
			List list = dstcToRoleManager.findDstcToRoleByCondition(prop);
			if (list != null) {
				for (int i = 0; i < list.size(); i++) {
					DstcToRole dstc = (DstcToRole) list.get(i);
					Long dstcId = dstc.getDstcId();
					Properties prop1 = new Properties();
					prop1.setProperty("id", dstcId.toString());
					List list2 = desktopContentManager
							.findDesktopContentByCondition(prop1);
					DesktopContent desktop = (DesktopContent) list2.get(0);
					list1.add(desktop);

				}
			}
			request.setAttribute("role", role);
			request.setAttribute("roles", roles);
			request.setAttribute("list", list1);

		} catch (JFossException e) {

			e.printStackTrace();
		}

		return DELINFO;

	}

	/**
	 * 查询出所有的桌面内容对象。和传去角色的ID。显示在页面上，修改该角色
	 */
	public String modifyInfo() throws Exception {
		String roleId = request.getParameter("role");

		List deskList = null;
		try {

			deskList = desktopContentManager.getAll();

			request.setAttribute("roleId", roleId);
			request.setAttribute("allDesktop", deskList);
		} catch (JFossException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return MODIFYINFO;
	}

	/**
	 * 首先先根据角色ID删除该角色所有的桌面内容对象。然后再从页面上取得的桌面内容对象的ID添加用户选择的 桌面内容对象到这个角色中
	 */
	public String addInfo() throws Exception {

		String deskIds[] = request.getParameterValues("dstcId");

		String roleId = request.getParameter("roleId");
		if (roleId != null && !roleId.equals("")) {
			try {
				dstcToRoleManager.deleteDstcToRole(new Long(roleId));
			} catch (NumberFormatException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (JFossException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		if(deskIds != null){
			for (int i = 0; i < deskIds.length; i++) {
				String deskId = deskIds[i];
				DstcToRole dst = new DstcToRole();
				dst.setDstcId(new Long(deskId));
				dst.setRoleId(new Long(roleId));
				try {
					dstcToRoleManager.addDstcToRole(dst);
				} catch (JFossException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		return ADDINFO;
	}
}
